<?php
class MysqliDB{
	// Conexion BBDD mysqli
	private $conn;
	private $host;
	private $user;
	private $password;
	private $databaseName;
	private $charset;
	
	public function __construct($host, $user, $password, $db, $port = null, $charset = null){
		if (is_array($host)){
			foreach ($host as $key=>$value){
				$$key = $val;
			}
		}
		
		if (is_object($host)){
			$this->conn = $host;
		}else{
			$this->host = $host;
		}
		$this->user = $user;
		$this->password = $password;
		$this->port = $port;
		$this->databaseName = $db;
		$this->charset = $charset;
		
	}
	
	public function createConnection(){
		if (isset($this->conn) && is_object($this->conn)){
			return $this->conn;
		}else{
			if (empty($this->host)){
				die ('Mysql host no ha sido definido');
			}
			if ($this->port){
				$this->conn = new mysqli($this->host, $this->user, $this->password, $this>databaseName, $this->port)
					or die('Error al crear la conexion');
			}else{
				$this->conn = new mysqli($this->host, $this->user, $this->password, $this>databaseName, $this->port)
					or die('Error al crear la conexion');
			}
			if ($this->charset){
				$this->conn->set_charset($this->charset);
			}
		}
	}
	
	public function rawQuery ($query, $bindParams = null, $sanitize = true){
		$this->_query = $query;
		if ($sanitize)
			$this->_query = filter_var ($query, FILTER_SANITIZE_STRING,
					FILTER_FLAG_NO_ENCODE_QUOTES);
		$stmt = $this->_prepareQuery();
		if (is_array($bindParams) === true) {
			$params = array(''); // Create the empty 0 index
			foreach ($bindParams as $prop => $val) {
				$params[0] .= $this->_determineType($val);
				array_push($params, $bindParams[$prop]);
			}
			call_user_func_array(array($stmt, 'bind_param'), $this->refValues($params));
		}
		$stmt->execute();
		$this->_stmtError = $stmt->error;
		$this->reset();
		return $this->_dynamicBindResults($stmt);
	}
	
	public function resetDB(){
		
	}
}
?>